package com.hyb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyb.entity.Product;
import com.hyb.mapper.ProductMapper;
import com.hyb.page.PageReq;
import com.hyb.page.VoQuery;
import com.hyb.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.corba.se.spi.orbutil.threadpool.WorkQueue;
import com.sun.xml.internal.ws.policy.spi.AbstractQNameValidator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  产品Service接口实现类
 * </p>
 *
 * @author hyb
 * @since 2021-12-29
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public void createProduct(Product product) {
        productMapper.insert(product);
    }

    @Override
    public PageReq getProducts(Integer limit, Integer current,String productType,String productSeries,String productBatch) {
        Page<Product> page = new Page<>(current,limit);
        VoQuery voQuery = new VoQuery();
        if (!StringUtils.isEmpty(productType)) {
            voQuery.setProductType(productType);
        }
        if (!StringUtils.isEmpty(productSeries)) {
            voQuery.setProductSeries(productSeries);
        }
        if (!StringUtils.isEmpty(productBatch)) {
            voQuery.setProductBatch(productBatch);
        }
        IPage<Product> products = productMapper.getProducts(page, voQuery);
        PageReq pageReq = new PageReq();
        pageReq.setTotal(products.getTotal());
        pageReq.setMyData(products.getRecords());
        return pageReq;
    }

    @Override
    public Product getProductById(Integer productId) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("product_id","product_series","product_model","product_type"
        ,"product_batch","product_unit","product_prices","product_remarks","product_url");
        queryWrapper.eq("product_id",productId);
        return productMapper.selectOne(queryWrapper);
    }

    @Override
    public void updateProduct(Product product) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",product.getProductId());
        productMapper.update(product,queryWrapper);
    }

    @Override
    public void deleteProduct(Integer productId) {
        productMapper.deleteById(productId);
    }

    @Override
    public Product selectProductByVo(String productSeries,String productModel,String productBatch) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_series",productSeries);
        queryWrapper.eq("product_model",productModel);
        queryWrapper.eq("product_batch",productBatch);
        queryWrapper.select("product_id","product_type","product_model","product_series","product_batch");
        return productMapper.selectOne(queryWrapper);
    }

    @Override
    public Set<Product> selectProductSeries(String productType) {

        return productMapper.selectProductSeries(productType);
    }

    @Override
    public List<Product> selectProductModels(String productSeries) {
        return productMapper.selectProductModels(productSeries);
    }

    @Override
    public Product getProductInfoByProductTypeAndProductSeriesAndProductModel(String productType, String productSeries, String productModel) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_series",productSeries);
        queryWrapper.eq("product_model",productModel);
        queryWrapper.eq("product_type",productType);
        return productMapper.selectOne(queryWrapper);
    }
}
